Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var _apiCode = _interopRequireDefault(require("./api-code")), _eventEmitter = _interopRequireDefault(require("event-emitter"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _typeof(e) {
    return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
        return typeof e;
    } : function(e) {
        return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
    })(e);
}

var socket = {
    URL: "wss://socket.ibos.cn",
    EVENT_PREFIX: "socket",
    EVENT_TYPES: {
        MESSAGE_CHAT: "event.chat",
        MESSAGE_CHAT_READ: "event.chat_message_read",
        MESSAGE_NOTIFY: "event.messagenotify",
        DATE_VOICE: "event.activity_create_voice_tips",
        VIEW_CAVRD_V2: "event.view_card_v2",
        CARD_FAV: "event.card_fav",
        ADD_CARD_LIKE: "event.add_card_like"
    },
    HEART_INTERVAL_TIME: 54e4,
    connect: function(e) {
        var n = this;
        this.connected || this.connecting || (this.connecting = !0, this.accesstoken = e, 
        wx.connectSocket({
            url: this.URL,
            header: {
                "content-type": "application/json"
            },
            method: "GET"
        }), wx.onSocketOpen(function(e) {
            console.log("socket 连接成功", e), n.connected = !0, n.connecting = !1, n.send({
                cmd: "api",
                route: "weapp/login",
                params: {
                    accesstoken: n.accesstoken
                }
            }), n.sendHeartMsg();
        }), wx.onSocketError(function(e) {
            console.error("socket 连接失败", e), n.connected = !1, n.connecting = !1, n.reconnect();
        }), wx.onSocketClose(function(e) {
            n.connected = !1, n.connecting = !1, e && e.code === _apiCode.default.SOCKET_AUTO_CLOSE ? (console.error("socket 自动关闭", e), 
            n.reconnect()) : console.error("socket 连接关闭", e);
        }), wx.onSocketMessage(function(e) {
            var t = e.data;
            try {
                t = JSON.parse(t);
            } catch (e) {}
            t.event ? _eventEmitter.default.emit("".concat(n.EVENT_PREFIX, ".event.").concat(t.event), t) : t.cmd && _eventEmitter.default.emit("".concat(n.EVENT_PREFIX, ".cmd.").concat(t.cmd), t), 
            console.log("socket 收到服务器内容：", t);
        }));
    },
    reconnect: function() {
        this.accesstoken && (wx.closeSocket(), this.connected = !1, console.log("socket 重新连接"), 
        this.connect(this.accesstoken));
    },
    send: function(e) {
        return new Promise(function(t, n) {
            wx.sendSocketMessage({
                data: "object" === _typeof(e) ? JSON.stringify(e) : e,
                success: function(e) {
                    return t(e);
                },
                fail: function(e) {
                    return n(e);
                }
            });
        });
    },
    sendHeartMsg: function() {
        var e = this;
        clearTimeout(this.heartTimer), this.heartTimer = setTimeout(function() {
            e.send("PING"), e.sendHeartMsg();
        }, this.HEART_INTERVAL_TIME);
    },
    on: function(e, t) {
        var n = [ "".concat(this.EVENT_PREFIX, ".").concat(e), t ];
        return _eventEmitter.default.addListener.apply(_eventEmitter.default, n), function() {
            _eventEmitter.default.removeListener.apply(_eventEmitter.default, n);
        };
    },
    once: function(e, t) {
        var n = this.on(e, function(e) {
            t && t(e), n();
        });
        return n;
    },
    close: function() {
        wx.closeSocket(), _eventEmitter.default.removeListener("".concat(this.EVENT_PREFIX, ".").concat(this.EVENT_TYPES.MESSAGE_CHAT));
    }
}, _default = socket;

exports.default = _default;